package cn.edu.ujn.multiple.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.ujn.multiple.dao.UserMapper;
import cn.edu.ujn.multiple.model.User;
import cn.edu.ujn.multiple.service.IUserService;
import cn.edu.ujn.multiple.util.Encrypt;
@Service("userService")
@Transactional
public class UserServiceImpl implements IUserService {

	@Autowired
	private UserMapper userDao;
	
	@Override
	public List<User> findAllUser() {
		// TODO Auto-generated method stub
		List<User> list = userDao.findAll();
		return list;
	}

	@Override
	public User findByPrimaryKey(Integer userId) {
		// TODO Auto-generated method stub
		User user = userDao.selectByPrimaryKey(userId);
		return user;
	}

	//登录验证方法
	@Override
	public User findUser(String userCode, String password) {
		// TODO Auto-generated method stub
		password = Encrypt.md5(password);
		User user = userDao.findUser(userCode, password);
		return user;
	}

	@Override
	public User insert(User user) {
		// TODO Auto-generated method stub
		user.setUserPassword(Encrypt.md5(user.getUserPassword()));
		if(user.getUserState()==null) {
			user.setUserState(1);
		}
		userDao.insertSelective(user);
		return user;
	}

	@Override
	public User update(User user) {
		// TODO Auto-generated method stub
		if(user.getUserPassword()!=Encrypt.md5(user.getUserPassword())) {
			user.setUserPassword(Encrypt.md5(user.getUserPassword()));
		}
		userDao.updateByPrimaryKeySelective(user);
		User updateUser = userDao.selectByPrimaryKey(user.getUserId());
		return updateUser;
	}

	@Override
	public int delete(User user) {
		// TODO Auto-generated method stub
		int i = userDao.deleteByPrimaryKey(user.getUserId());
		return i;
	}

	@Override
	public User findByCode(User user) {
		// TODO Auto-generated method stub
		List<User> list = userDao.findByCode(user);
		return list.get(0);
	}

}
